package com.foreknow.service.impl;

import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import com.foreknow.dao.FmeditemMapper;
import com.foreknow.entity.Department;
import com.foreknow.entity.Expenseclass;
import com.foreknow.entity.Fmeditem;
import com.foreknow.service.FmeditemService;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class FmeditemServiceImpl implements FmeditemService {
    @Autowired
    private FmeditemMapper fmeditemMapper;
    @Override
    public List<Fmeditem> queryFmeditemArr() {
        return fmeditemMapper.queryFmeditemArr();
    }

    @Override
    public List<Expenseclass> queryExpItem() {
        return fmeditemMapper.queryExpItem();
    }

    @Override
    public List<Department> queryDeptItem() {
        return fmeditemMapper.queryDeptItem();
    }

    @Override
    public List<Fmeditem> selectFmed(Fmeditem fmeditem) {
        return fmeditemMapper.selectFmed(fmeditem);
    }


    @Override
    public int insert(Fmeditem fmeditem) {
        return fmeditemMapper.insert(fmeditem);
    }

    @Override
    public Fmeditem selectByPrimaryKey(Integer id) {
        return fmeditemMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKey(Fmeditem fmeditem) {
        return fmeditemMapper.updateByPrimaryKey(fmeditem);
    }

    @Override
    public int deleteFmedById(int id) {
        return fmeditemMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int excelToList(InputStream inputStream) throws IOException,  org.apache.poi.openxml4j.exceptions.InvalidFormatException {
        List<Fmeditem> gisList = new ArrayList<>();
        Workbook workbook = null;
        workbook = WorkbookFactory.create(inputStream);
        inputStream.close();
        //工作表对象
        Sheet sheet = workbook.getSheetAt(0);
        //总行数
        int rowLength = sheet.getLastRowNum() + 1;
        //工作表的列
        Row row = sheet.getRow(0);
        //总列数
        int colLength = row.getLastCellNum();
        for (int i = 2; i < rowLength; i++) {
            row = sheet.getRow(i);
            row.getCell(0).setCellType(CellType.STRING);
            row.getCell(1).setCellType(CellType.STRING);
            row.getCell(2).setCellType(CellType.STRING);
            row.getCell(3).setCellType(CellType.STRING);
            row.getCell(4).setCellType(CellType.STRING);
            row.getCell(5).setCellType(CellType.STRING);
            row.getCell(6).setCellType(CellType.STRING);
            row.getCell(7).setCellType(CellType.STRING);
            row.getCell(8).setCellType(CellType.STRING);

            String cell0 = row.getCell(0).getStringCellValue().trim();;
            if(cell0==""){
                break;
            }
            String cell1 = row.getCell(1).getStringCellValue().trim();
            String cell2 = row.getCell(2).getStringCellValue().trim();
            String cell3 =row.getCell(3).getStringCellValue().trim();
            double cell4 = Double.parseDouble(row.getCell(4).getStringCellValue().trim());
            Integer cell5 = Integer.parseInt(row.getCell(5).getStringCellValue().trim());
            Integer cell6 = Integer.parseInt(row.getCell(6).getStringCellValue().trim());
            String cell7 = row.getCell(7).getStringCellValue().trim();
            Integer cell8 = Integer.parseInt(row.getCell(8).getStringCellValue().trim());

            Fmeditem fmeditem=new Fmeditem();
            fmeditem.setItemcode(cell1);
            fmeditem.setItemname(cell2);
            fmeditem.setFormat(cell3);
            fmeditem.setPrice(cell4);
            fmeditem.setExpclassid(cell5);
            fmeditem.setDeptid(cell6);
            fmeditem.setMnemoniccode(cell7);
            fmeditem.setRecordtype(cell8);
            fmeditem.setCreationdate(new Date());
            fmeditem.setLastupdatedate(null);
            fmeditem.setDelmark(1);

            gisList.add(fmeditem);
        }
        return fmeditemMapper.insertMultiGis(gisList);
    }
}
